import request from "@/utils/request";
import type { AxiosPromise } from "axios";
import type { LoginData, LoginResult, TenantInfo, VerifyCodeResult } from "./types";
import type { UserInfo } from "@/api/system/user/types";
import { getRefreshToken } from "@/utils/auth";

const clientId = import.meta.env.VITE_APP_CLIENT_ID;

/**
 * 注销
 * @param refreshToken 刷新token
 * @returns
 */
export function logout(refreshToken: string) {
  return request({
    url: "/auth/logout/" + refreshToken,
    method: "delete"
  });
}

/**
 * 用户登陆
 * @param data {LoginData} 登陆信息
 * @returns
 */
export function login(data: LoginData): AxiosPromise<LoginResult> {
  const params = {
    ...data,
    clientId: data.clientId || clientId,
    grantType: data.grantType || "password"
  };
  return request({
    url: "/auth/login",
    headers: {
      isToken: false
    },
    method: "post",
    data: params
  });
}

/**
 * 获取用户详细信息
 */
export function getInfo(): AxiosPromise<UserInfo> {
  return request({
    url: "/system/user/getInfo",
    method: "get"
  });
}

/**
 * 获取验证码开启状态
 */
export function getCaptchaEnable(): AxiosPromise<VerifyCodeResult> {
  return request({
    url: "/getCaptchaStatus",
    headers: {
      isToken: false
    },
    method: "get"
  });
}

/**
 * 获取租户列表
 */
export function getTenantList(): AxiosPromise<TenantInfo> {
  return request({
    url: "/auth/tenant/list",
    headers: {
      isToken: false
    },
    method: "get"
  });
}

/**
 * 无感刷新Token
 */
export function refreshToken(): AxiosPromise<any> {
  return request({
    url: "/auth/refreshToken/" + getRefreshToken(),
    headers: {
      isToken: false
    },
    method: "get"
  });
}
